// 区县管理页面
<template>
  <div class="app-container">
    <div class="cbox">
      <el-form
        :inline="true"
        :model="compForm"
        ref="compForm"
        size="mini"
        :rules="compFormRules"
      >
        <!-- <el-row> -->
          <!-- <el-col :span="4">/ -->
            <el-form-item label="省中文名称:" prop="shengName">
              <el-select
                filterable
                allow-create
                clearable
                v-model="compForm.shengName"
                @change="change"
                style="width:150px"
              >
                <el-option
                  v-for="item in shengxl"
                  :key="item.PRV_CODE"
                  :value="item.PRV_CODE"
                  :label="item.PRV_CHNNAME"
                ></el-option>
              </el-select>
            </el-form-item>
          <!-- </el-col> -->
          <!-- <el-col :span="4"> -->
            <el-form-item label="市中文名称:" prop="shiName">
              <el-select filterable allow-create clearable style="width:150px" v-model="compForm.shiName">
                <el-option
                  v-for="item in shixlA"
                  :key="item.CTYE_CODE"
                  :value="item.CTYE_CODE"
                  :label="item.CTYE_CHNNAME"
                ></el-option>
              </el-select>
            </el-form-item>
          <!-- </el-col> -->
          <!-- <el-col :span="4"> -->
            <el-form-item label="区县代码:" prop="code">
              <el-input clearable maxlength="16" style="width:150px" v-model="compForm.code"></el-input>
            </el-form-item>
          <!-- </el-col> -->
          <!-- <el-col :span="4"> -->
            <el-form-item label="区县中文名称:" prop="qxName">
              <el-input clearable maxlength="16" style="width:150px" v-model="compForm.qxName"></el-input>
            </el-form-item>
          <!-- </el-col> -->
          <!-- <el-col :span="2"> -->
            <el-form-item >
              <el-button type="primary" @click="search">查询</el-button>
            </el-form-item>
          <!-- </el-col> -->
        <!-- </el-row> -->
      </el-form>
    </div>
    <el-row :gutter="10" class="btn-area">
      <el-col :span="24">
        <buttonName @clickEvent="clickEventDom"></buttonName>
        <!-- <el-button size="mini" type="primary" plain v-if="isAuth('add')" @click="add">新增区县</el-button>
        <el-button size="mini" type="primary" plain v-if="isAuth('edit')" @click="edit">编辑区县</el-button>
        <el-button size="mini" type="primary" plain v-if="isAuth('del')" @click="del">删除区县</el-button> -->
      </el-col>
    </el-row>
    <div class="table">
      <div class="inner" ref="inner">
        <el-table
          :data="compList"
          :highlight-current-row="true"
          @row-click="getCurrentRow"
          @row-dblclick="storageDetail"
          border
          :header-cell-style="{
            textAlign: 'center'
          }"
          :height="maxHeight"
        >
          <el-table-column
            align="center"
            :index="indexMethod"
            label="序号"
            type="index"
            show-overflow-tooltip
            width="60"
          ></el-table-column>
          <el-table-column
            size="mini"
            align="left"
            label="区县代码"
            prop="ctyCode"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            size="mini"
            align="left"
            label="省中文名称"
            width="110"
            prop="prvChnName"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            size="mini"
            align="left"
            label="市中文名称"
             width="110"
            prop="ctyCityName"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            size="mini"
            align="left"
            label="区县中文名称"
             width="110"
            prop="ctyChnname"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            size="mini"
            align="left"
            label="英文名称"
            prop="ctyEgsname"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            size="mini"
            align="left"
            label="首字母"
            prop="ctyPrename"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            size="mini"
            align="left"
            label="创建员工"
            prop="ctyInsertuser"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            size="mini"
            align="center"
            label="创建时间"
            prop="ctyInsertdt"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            size="mini"
            align="left"
            label="最后输入员工"
             width="130"
            prop="ctyLstupduser"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            size="mini"
            align="center"
            label="最后输入时间"
             width="130"
            prop="ctyLstupddt"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column align="left" label="是否有效" prop="ctyIsvalid" show-overflow-tooltip>
            <template slot-scope="scope">
              <div style="text-align: center;" v-if="scope.row.ctyIsvalid == 'Y'">
                <el-checkbox v-model="item.item" disabled></el-checkbox>
              </div>
              <div style="text-align: center;" v-if="scope.row.ctyIsvalid == 'N'">
                <el-checkbox v-model="item.itemA" disabled></el-checkbox>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <el-pagination
      class="pagination"
      small
      background
      ref="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="Pagination.currentPage"
      :page-sizes="Pagination.pageSizeList"
      :page-size="Pagination.pageSize"
      :layout="Pagination.layout"
      :total="Pagination.total"
    ></el-pagination>

    <el-dialog
      width="700px"
      v-el-drag-dialog
      ref="dialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      v-if="showDialog"
      :title="operate"
      :before-close="handleClose"
    >
      <el-form
        :model="dialogForm"
        ref="dialogForm"
        label-width="13rem"
        size="mini"
        :rules="dialogFormRules"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="省中文名称：" prop="ctyPrvCode">
              <el-select
                v-model="dialogForm.ctyPrvCode"
                :disabled="disabled"
                clearable
                filterable
                @change="changeA"
              >
                <el-option
                  v-for="i in shengxl"
                  :key="i.PRV_CODE"
                  :label="i.PRV_CHNNAME"
                  :value="i.PRV_CODE"
                  :title="i.PRV_CHNNAME"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="市中文名称：" prop="ctyCityCode">
              <el-select v-model="dialogForm.ctyCityCode" :disabled="disabled" clearable filterable>
                <el-option
                  v-for="i in shixl"
                  :key="i.CTYE_CODE"
                  :label="i.CTYE_CHNNAME"
                  :value="i.CTYE_CODE"
                  :title="i.CTYE_CHNNAME"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="区县代码：" prop="ctyCode">
              <el-input
                minlength="1"
                maxlength="6"
                clearable
                v-model="dialogForm.ctyCode"
                @change="uppercase('ctyCode')"
                :disabled="disabledA"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区县中文名称：" prop="ctyChnname">
              <el-input
                minlength="1"
                maxlength="16"
                clearable
                :disabled="disabled"
                v-model="dialogForm.ctyChnname"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="英文名称：" prop="ctyEgsname">
              <el-input
                minlength="1"
                maxlength="16"
                clearable
                :disabled="disabled"
                v-model="dialogForm.ctyEgsname"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="是否有效："
              :disabled="disabled"
              :prop="disabled == false ? 'ctyIsvalid' : ''"
            >
              <el-radio-group
                :disabled="disabled"
                v-model="dialogForm.ctyIsvalid"
              >
                <el-radio label="Y">是</el-radio>
                <el-radio label="N">否</el-radio>
              </el-radio-group>
              <!-- <el-select v-model.trim="dialogForm.CTY_ISVALID">
                            <el-option v-for="item in options" :key="item.value" :label="item.label" :title="item.label" :value="item.value">
                            </el-option>
              </el-select>-->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="首字母：" prop="ctyPrename">
              <el-input
                minlength="1"
                maxlength="1"
                clearable
                :disabled="disabled"
                @change="uppercase('ctyPrename')"
                v-model="dialogForm.ctyPrename"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="24">
            <el-form-item prop="ctyRemark" label="备注：">
              <el-input
                maxlength="32"
                :disabled="disabled"
                type="textarea"
                v-model="dialogForm.ctyRemark"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="close('dialogForm')">关闭</el-button>
        <el-button size="mini" v-if="see" @click="save" type="primary">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  districtCountyList,
  addShippingCompany,
  editShippingCompany,
  delShippingCompany,
  shengCityManagementXL,
  shiShippingCompany
} from '@/request/api'
import resizeMixin from '@/mixins/resize'
import buttonName from '@/components/buttonName/button-name.vue'
import isAuth from '@/mixins/buttonAuth'
import paginationMixin from '@/mixins/pagination'

export default {
  name: 'router.districtCounty',
  mixins: [resizeMixin, paginationMixin,isAuth],
  data() {
    return {
      item:{
        item:true,
        itemA:false
      },
      options: [
        {
          value: 'Y',
          label: '激活'
        },
        {
          value: 'N',
          label: '未激活'
        }
      ],
      disabledA: false,
      disabled: false,
      see: true,
      dialogFormStatus: '', // dialog 中 form 的状态 add/edit
      currentData: {},
      showDialog: false,
      maxHeight: -1,
      operate: '',
      compForm: {
        shengName: '',
        shiName: '',
        code: '',
        qxName: ''
      },
      compFormRules: {
        cName: [
          {
            pattern: /^[\u4e00-\u9fa5]+$/,
            message: '只能输入中文汉字',
            trigger: 'change'
          }
        ]
      },
      dialogForm: {},
      dialogFormRules: {
        ctyCityCode: [
          {
            required: true,
            message: '市中文名称不能为空',
            trigger: 'change'
          }
        ],
        ctyPrvCode: [
          {
            required: true,
            message: '省中文名称不能为空',
            trigger: 'change'
          }
        ],
        ctyChnname: [
          {
            required: true,
            message: '区县中文名称不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^[\u4e00-\u9fa5]+$/,
            message: '请输入中文',
            trigger: 'blur'
          }
        ],
        ctyEgsname: [
          {
            required: true,
            message: '英文名称不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z ]+$/,
            message: '请输入英文',
            trigger: 'blur'
          }
        ],
        ctyPrename: [
          {
            required: true,
            message: '拼音首字母不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z]/,
            message: '请输入英文',
            trigger: 'blur'
          }
        ],
        ctyCode: [
          {
            required: true,
            message: '区县代码不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z0-9]+$/,
            message: '请输入英文和数字',
            trigger: 'blur'
          }
        ]
      },
      compList: [],
      shengxl: [],
      shixl: [],
      shixlA: [],
      box: ''
    }
  },
  mounted() {
    this.refreshList()
    // this.dataList()
    this.shengData()
    //this.shiDataA()
    this.change()
    this.changeA()
  },
  created() {
    this.resize = () => {
      this.maxHeight = this.$refs.inner.offsetHeight
    }
  },
  methods: {
    // 生成表格序号的方法
    indexMethod (index) {
      return 1 + index + (Number(this.Pagination.currentPage) - 1) * Number(this.Pagination.pageSize)
    },
    //弹出框 市下拉框数据
    changeA(row) {
      this.box = row
      //市下拉框数据
      shiShippingCompany({ ctyeCode: '', ctyeCityId: this.box }).then(res => {
        this.shixl = res.data
        // this.shixlA = res.data.FTKJWLHYPT
      })
    },
    //页面市下拉框数据
    change(row) {
      this.box = row
      //市下拉框数据
      shiShippingCompany({ ctyeCode: '', ctyeCityId: this.box }).then(res => {
        // this.shixl = res.data.FTKJWLHYPT
        this.shixlA = res.data
      })
    },
    //省下拉框
    shengData() {
      shengCityManagementXL().then(res => {
        this.shengxl = res.data
      })
    },
    uppercase(key) {
      this.dialogForm[key] = this.dialogForm[key].toUpperCase()
    },
    trimECode() {
      this.dialogForm.ctyEgsname = this.dialogForm.ctyEgsname.trim()
    },

    // 点击查询
    search() {
      // this.searchList()
      this.refreshList()
    },

    //获取列表数据
    getList(currentPage, pageSize, updateTotal) {
      districtCountyList({
        ctyCode: this.compForm.code,
        ctyChnname: this.compForm.qxName,
        ctyCityCode: this.compForm.shiName,
        ctyPrvCode: this.compForm.shengName,
        PageIndexs: this.Pagination.currentPage.toString(),
        PageSizes: this.Pagination.pageSize.toString()
      }).then(response => {
        this.compList = response.data
        updateTotal(Number(response.total))
      })
    },
    // 新增
    add() {
      this.showDialog = true
      this.operate = '新增区县信息'
      this.shixl = []
      this.see = true
      this.disabled = false
      this.disabledA = false
      this.dialogFormStatus = 'add'
      if (this.$refs['dialogForm']) {
        this.$refs['dialogForm'].resetFields()
      }
      this.resetDialogForm()
    },
    //编辑
    edit() {
      this.dialogFormStatus = 'edit'
      if (Object.keys(this.currentData).length == 0) {
        this.$alert('请选择需要编辑的数据！', '提示', {
          confirmButtonText: '确定'
        })
        return
      }

      this.showDialog = true
      this.operate = '编辑区县信息'
      this.see = true
      this.disabled = false;
      (this.disabledA = true),
      (this.dialogForm = Object.assign({}, this.currentData))
      new Promise(resolve => {
        resolve('')
      })
        .then(() => {
          // this.getCountryList()
        })
        // .then(() => {
        //   this.getChanges()
        // })
        .then(() => {
          this.$refs.dialogForm.clearValidate()
        })
    },
    //删除
    del() {
      if (Object.keys(this.currentData).length == 0) {
        this.$alert('请选择需要删除的数据', '提示', {
          confirmButtonText: '确定'
        })
        return
      }

      this.$confirm('您确认要删除此数据吗?', '提示', {
        closeOnClickModal: false
      })
        .then(() => {
          delShippingCompany({ ctyCode: this.currentData.ctyCode }).then(() => {
            this.$message.success({
              message: '删除成功！',
              showClose: true
            })
            this.refreshList()
          })
        })
        .catch(() => {})
    },
    save() {
      // this.trimECode()
      if (this.dialogFormStatus === 'add') {
        this.commitAdd('dialogForm')
      } else if (this.dialogFormStatus === 'edit') {
        this.commitEdit('dialogForm')
      }
    },
    commitAdd(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          addShippingCompany(this.dialogForm).then(() => {
            this.$message.success({
              message: '新增数据成功！',
              showClose: true
            })
            this.showDialog = false
            this.refreshList()
          })
        }
      })
    },
    commitEdit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          editShippingCompany(this.dialogForm).then(() => {
            this.$message.success({
              message: '编辑数据成功！',
              showClose: true
            })
            this.showDialog = false
            this.currentData = {}
            this.refreshList()
          })
        }
      })
    },
    close(formName) {
      this.$refs[formName].resetFields()
      this.dialogForm = {}
      this.currentData = {}
      this.refreshList()
      this.showDialog = false
    },
    resetDialogForm() {
      this.dialogForm = {
        ctyPrvCode: '',
        // prvPrvCode:'', //省中文名称
        ctyCityCode: '', //市中文名称
        ctyChnname: '', // 区县中文名称
        ctyCode: '', //区县代码
        ctyEgsname: '', //英文名称
        ctyRemark: '',
        ctyInsertuser: '',
        ctyIsvalid: 'Y',
        ctyPrename:''
      }
    },
    // 获取当前选中行
    getCurrentRow(row) {
      this.currentData = row
    },
    //双击查看
    storageDetail() {
      this.showDialog = true
      this.operate = '查看区县信息'
      this.see = false
      this.disabled = true
      this.disabledA = true
      this.dialogForm = Object.assign({}, this.currentData)
    },
    handleClose(done) {
      if (this.$refs.dialogForm) {
        this.$refs.dialogForm.resetFields()
        done()
      }
    },
  //调取父组件方法
    clickEventDom(item){
      if(item.mdlCode === 'add'){
        this.add()
      }else if(item.mdlCode === 'edit'){
        this.edit()
      }else if(item.mdlCode === 'del'){
        this.del()
      }
    }
  },
  components: {
    buttonName
  },
}
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}

.app-container {
  height: 100%;
  width: 100%;
  position: relative;
  display: flex;
  flex-direction: column;

  .table {
    flex: 1;
    position: relative;
    overflow: hidden;

    .inner {
      width: 100%;
      height: 100%;
      overflow: hidden;
    }

    .el-pagination {
      height: 4rem;
      flex: 0 0 auto;
      margin: 1rem 0;
    }
  }
}
</style>
